package com.neusoft.nep.admin.controller; // 假设在admin.controller包中

import com.neusoft.nep.admin.entity.AqiData; // 导入 AqiData 类
import com.neusoft.nep.common.AqiDataFileUtil; // 导入文件工具类

import javafx.fxml.FXML;
import javafx.scene.control.TextField;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class OtherTotalStatisticsViewController { // 假设这是您的控制器类名

    @FXML
    private TextField total; // 对应 "空气质量检测总数量"
    @FXML
    private TextField good;  // 对应 "空气质量检测良好数量"
    @FXML
    private TextField range; // 对应 "省网格覆盖率"
    @FXML
    private TextField bigCityRange; // 对应 "大城市网格覆盖范围"


    private static final Set<String> BIG_CITIES = new HashSet<>(List.of(
            "北京市", "上海市", "广州市", "深圳市", "天津市", "重庆市", "成都市", "武汉市",
            "杭州市", "南京市", "沈阳市", "大连市", "太原市", "长沙市", "吉林市", "长春市" // 将之前数据中出现的大城市也加入
    ));

    @FXML
    public void initialize() {
        loadAndDisplayStatistics();
    }

    private void loadAndDisplayStatistics() {
        List<AqiData> allAqiData = AqiDataFileUtil.readAqiDataFromFile();

        int totalCount = 0;
        int goodCount = 0;
        Set<String> coveredProvinces = new HashSet<>();
        Set<String> coveredBigCities = new HashSet<>();

        if (allAqiData.isEmpty()) {
            System.out.println("数据文件 fankui3.txt 中没有数据可供统计。");
            // 设置所有TextField为0或空
            total.setText("0");
            good.setText("0");
            range.setText("0个省份");
            bigCityRange.setText("0个大城市");
            return;
        }

        totalCount = allAqiData.size();

        for (AqiData data : allAqiData) {
            // 统计良好数量
            String level = data.getLevel();
            if (level != null && (level.equals("优") || level.equals("良"))) {
                goodCount++;
            }

            // 统计省份覆盖
            String province = data.getProvince();
            if (province != null && !province.isEmpty()) {
                coveredProvinces.add(province);
            }

            // 统计大城市覆盖
            String city = data.getCity();
            if (city != null && !city.isEmpty() && BIG_CITIES.contains(city)) {
                coveredBigCities.add(city);
            }
        }

        // 更新TextFields
        total.setText(String.valueOf(totalCount));
        good.setText(String.valueOf(goodCount));
        double provincesPercentage = (double) coveredProvinces.size() / 34 * 100;
        range.setText(String.format("%.2f%%", provincesPercentage));

        double bigCitiesPercentage = (double) coveredBigCities.size() / 16 * 100;
        bigCityRange.setText(String.format("%.2f%%", bigCitiesPercentage)); // 显示覆盖的大城市数量

        System.out.println("统计数据加载并展示完成。");
    }
}